package edu.thu.studio.ormdesigner.editor.command;

import edu.thu.studio.ormdesigner.common.AbstractCommand;
import edu.thu.studio.ormdesigner.model.diagram.Diagram;
import edu.thu.studio.ormdesigner.model.diagram.node.ViewNode;
import edu.thu.studio.ormdesigner.model.orm.view.View;
import edu.thu.studio.ormdesigner.util.Utils;

public class DeleteViewNodeCommand extends AbstractCommand {
    
    private Diagram diagram;
    
    private View view;
    
    private ViewNode node;
    
    public DeleteViewNodeCommand(Diagram diagram, ViewNode node) {
        this.diagram = diagram;
        this.node = node;
    }
    
    @Override
    protected void doExecute() {
        this.diagram.getNodes().remove(node);
        if (node.isOriginal()) {
            view = Utils.getView(node.getRefId());
            Utils.getViewSet().remove(view);
        }
        this.diagram.refreshChildren();
    }
    
    @Override
    protected void doUndo() {
        this.diagram.getNodes().addObject(node);
        if (node.isOriginal()) {
            Utils.getViewSet().addObject(view);
        }
        this.diagram.refreshChildren();
    }
}
